package com.clannad.staservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.clannad.commonutils.R;
import com.clannad.servicebase.exceptionhandler.GuliException;
import com.clannad.staservice.entity.StatisticsDaily;
import com.clannad.staservice.feign.UcenterClient;
import com.clannad.staservice.mapper.StatisticsDailyMapper;
import com.clannad.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author clannad
 * @since 2022-08-23
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;
    @Override
    public void registerCount(String day) {
        LambdaQueryWrapper<StatisticsDaily> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StatisticsDaily::getDateCalculated,day);
        this.remove(lambdaQueryWrapper);
        R r = ucenterClient.countRegister(day);
        Integer countRegister = (Integer) r.getData().get("countRegister");
        StatisticsDaily statisticsDaily=new StatisticsDaily();
        statisticsDaily.setRegisterNum(countRegister);
        statisticsDaily.setDateCalculated(day);
        statisticsDaily.setVideoViewNum(RandomUtils.nextInt(100,200));
        statisticsDaily.setLoginNum(RandomUtils.nextInt(100,200));
        statisticsDaily.setCourseNum(RandomUtils.nextInt(100,200));
        this.save(statisticsDaily);
    }

    @Override
    public Map<String, Object> showData(String type, String begin, String end) {
        if(StringUtils.isEmpty(type)|| StringUtils.isEmpty(begin) || StringUtils.isEmpty(end)){
            throw new GuliException(20001,"请填写相关信息");
        }
        QueryWrapper<StatisticsDaily> queryWrapper=new QueryWrapper<>();
        queryWrapper.between("date_calculated",begin,end);
        queryWrapper.select("date_calculated",type);
        List<StatisticsDaily> staList = this.list(queryWrapper);
        List<String> date_calculated=new ArrayList<>();
        List<Integer> numDataList=new ArrayList<>();

        for (StatisticsDaily statisticsDaily : staList) {
            date_calculated.add(statisticsDaily.getDateCalculated());
            switch (type){
                case "login_num":
                    numDataList.add(statisticsDaily.getLoginNum());
                    break;
                case "register_num":
                    numDataList.add(statisticsDaily.getRegisterNum());
                    break;
                case "video_view_num":
                    numDataList.add(statisticsDaily.getVideoViewNum());
                    break;
                case "course_num":
                    numDataList.add(statisticsDaily.getCourseNum());
                    break;
                default:break;
            }
        }
        Map<String ,Object> map=new HashMap<>();
        map.put("date_calculated",date_calculated);
        map.put("numDataList",numDataList);
        return map;
    }
}
